import java.util.List;

class Node{
    public int val;
    public Node next;
    public Node(int val){
        this.val=val;
    }
}
public class MyLinkedList {
    public Node head;
    public void craateList(){
        Node node1=new Node(12);
        Node node2=new Node(23);
        Node node3=new Node(34);
        Node node4=new Node(45);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        head=node1;
    }

    public void display(){
        Node cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    public boolean contains(int key){
        Node cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    public int size(){
        int count=0;
        Node cur=head;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public void addFirst(int data){
        Node node=new Node(data);
        if(head==null){
            head=node;
        }else{
            node.next=head;
            head=node;
        }
    }
    public void addLast(int data){
        Node node=new Node(data);
        if(head==null){
            head=node;
        }else{
            Node cur=head;
            while(cur.next!=null){
                cur=cur.next;
            }
            cur.next=node;
        }
    }

    public void addIndex(int index,int data){
        if(index<0||index>size()){
            System.out.println("index位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        Node cur=findNode(index);
        Node node=new Node(data);
        node.next=cur.next;
        cur.next=node;
    }
    public Node findNode(int index){
        Node cur=head;
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }

    public void remove(int key){
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        Node cur=searchPrev(key);
        if(cur==null){
            return;
        }
        Node del=cur.next;
        cur.next=del.next;

    }
    public Node searchPrev(int key){
        Node cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    public Node removeAllKey(int key){
        if(head==null){
            return null;
        }
        Node prev=head;
        Node cur=head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
        return head;

    }

    public void clear(){
        while(head!=null){
            Node curNext=head.next;
            head.next=null;
            head=curNext;
        }

    }
}
